package com.qjs.smterp.web.beans;

import java.io.Serializable;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;

import org.primefaces.event.CloseEvent;
import org.primefaces.event.SelectEvent;

import com.qjs.smterp.model.Users;
import com.qjs.smterp.types.UserRole;
import com.qjs.smterp.services.SecurityServices;
import com.qjs.smterp.utils.MD5;
import com.qjs.smterp.utils.TextUtil;
import com.qjs.smterp.web.utils.FacesContextUtils;

@ManagedBean(name = "mbConfiguration")
@ViewScoped
public class MbConfiguration implements Serializable {

	private static final long serialVersionUID = 1L;
	private static final String DEFAULT_PASSWORD = "12345678";

	@ManagedProperty("#{securityServices}")
	private SecurityServices securityServices;

	private List<Users> users;

	private Users selectedUser;

	private boolean editUserMode;

	private Users userLogin;

	@PostConstruct
	protected void init() {
		userLogin = (Users) FacesContextUtils
				.getSessionObject(MbAuthentication.AUTH_KEY);
		getUsersList();
		selectedUser = new Users();
	}

	private void getUsersList() {
		if (userLogin.getRole().equals(UserRole.ADMIN.name())) {
			users = securityServices.findAll();
		}
	}

	public String prepareAddUser() {
		selectedUser = new Users();
		editUserMode = false;
		return null;

	}

	public void resetPassword() {
		selectedUser.setPwd(DEFAULT_PASSWORD);
		String encryptPass = MD5.MD5Encode(selectedUser.getPwd());
		selectedUser.setPwd(encryptPass);
		securityServices.updateUsers(selectedUser);
		FacesContextUtils.addInfoMessage(String.format(
				"The password of user[ %s ] have been reseted.",
				selectedUser.getLogin()));
	}

	public void handleClose(CloseEvent event) {
		selectedUser = new Users();
		editUserMode = false;
	}

	public String prepareEditUser() {
		editUserMode = true;
		return null;

	}

	public String deleteUser() {
		if (selectedUser != null) {
			securityServices.delete(selectedUser);
		}
		getUsersList();
		editUserMode = false;
		return null;
	}

	public void onRowUserSelect(SelectEvent selectEvent) {
		this.editUserMode = true;
	}

	public void saveUser() {
		if (editUserMode) {
			editUser();
			return;
		}
		String password = selectedUser.getPwd();
		if (TextUtil.isNotEmpty(password) && password.length() < 8) {
			FacesContextUtils
					.addErrorExceptionMessage("The password is must more than or equal 8 character.");
			return;
		}
		String encryptPass = MD5.MD5Encode(selectedUser.getPwd());
		selectedUser.setPwd(encryptPass);
		securityServices.updateUsers(selectedUser);
		selectedUser = new Users();
		getUsersList();
		editUserMode = false;
	}

	public void editUser() {
		securityServices.updateUsers(selectedUser);
		selectedUser = new Users();
		getUsersList();
		editUserMode = false;
	}

	public List<Users> getUsers() {
		return users;
	}

	public void setUsers(List<Users> users) {
		this.users = users;
	}

	public boolean isEditUserMode() {
		return editUserMode;
	}

	public void setEditUserMode(boolean editUserMode) {
		this.editUserMode = editUserMode;
	}

	public Users getSelectedUser() {
		return selectedUser;
	}

	public void setSelectedUser(Users selectedUser) {
		this.selectedUser = selectedUser;
	}

	public void setSecurityServices(SecurityServices securityServices) {
		this.securityServices = securityServices;
	}

	public UserRole[] getUserRoles() {
		return UserRole.values();
	}

	public Users getUserLogin() {
		return userLogin;
	}

	public void setUserLogin(Users userLogin) {
		this.userLogin = userLogin;
	}

	public boolean isAdminRole() {
		return userLogin.getRole().equals(UserRole.ADMIN.name());
	}
}
